package com.xgy.ordering.controller;

import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import com.xgy.ordering.service.PerformanceService;
import com.xgy.ordering.dto.TestRequest;
import com.xgy.ordering.dto.TestResults;
import java.util.*;
import java.util.HashMap;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@RestController
@RequestMapping("/api/performance")
public class PerformanceController {

    private static final Logger logger = LoggerFactory.getLogger(PerformanceController.class);

    @Autowired
    private PerformanceService performanceService;

    @PostMapping("/test")
    public Map<String, Object> runPerformanceTest(@RequestBody TestRequest request) {
        logger.info("接收到性能测试请求 - 数据大小: {}, 测试类型: {}, 迭代次数: {}", 
                   request.getDataSize(), request.getTestType(), request.getIterations());
        
        Map<String, Object> result = new HashMap<>();

        try {
            TestResults testResults = performanceService.executePerformanceTest(
                    request.getDataSize(), request.getTestType(), request.getIterations());

            result.put("success", true);
            result.put("data", testResults);
            result.put("message", "性能测试完成");
            
            logger.info("性能测试完成 - 数据大小: {}, 测试类型: {}", 
                       request.getDataSize(), request.getTestType());

        } catch (Exception e) {
            logger.error("性能测试执行失败 - 数据大小: {}, 测试类型: {}, 错误: {}", 
                        request.getDataSize(), request.getTestType(), e.getMessage(), e);
            result.put("success", false);
            result.put("message", "测试执行失败: " + e.getMessage());
        }

        return result;
    }

    @GetMapping("/comparison")
    public Map<String, Object> getComparisonData() {
        logger.info("获取性能对比数据");
        return performanceService.getComparisonData();
    }

    @GetMapping("/smartlist/info")
    public Map<String, Object> getSmartListInfo() {
        logger.info("获取SmartList信息");
        Map<String, Object> info = new HashMap<>();
        info.put("name", "SmartList");
        info.put("description", "智能混合存储列表实现");
        info.put("features", new String[]{
                "小数据使用数组存储",
                "大数据自动切换分段存储",
                "动态负载均衡",
                "智能容量管理"
        });
        info.put("threshold", 1000);
        info.put("bucketSize", 500);

        return info;
    }
}